package com.qlyl.modules.counsel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qlyl.common.api.vo.Result;
import com.qlyl.common.aspect.annotation.AutoLog;
import com.qlyl.common.system.base.controller.JeecgController;
import com.qlyl.common.system.query.QueryGenerator;
import com.qlyl.common.util.MakeHtmlUtil;
import com.qlyl.modules.counsel.entity.Banners;
import com.qlyl.modules.counsel.service.IBannersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: Banners
 * @Author: jeecg-boot
 * @Date:   2020-01-08
 * @Version: V1.0
 */
@Slf4j
@Api(tags="Banners")
@RestController
@RequestMapping("/banners")
public class BannersController extends JeecgController<Banners, IBannersService> {
	@Autowired
	private IBannersService bannersService;

	 @Value(value = "${jeecg.path.webapp}")
	 private String webappPath;
	@Value(value = "${jeecg.path.mbPath}")
	private String mbPath;
	
	/**
	 * 分页列表查询
	 *
	 * @param banners
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "Banners-分页列表查询")
	@ApiOperation(value="Banners-分页列表查询", notes="Banners-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Banners banners,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Banners> queryWrapper = QueryGenerator.initQueryWrapper(banners, req.getParameterMap());
		Page<Banners> page = new Page<Banners>(pageNo, pageSize);
		IPage<Banners> pageList = bannersService.page(page, queryWrapper);
		for (Banners banner:pageList.getRecords()){
			String html = MakeHtmlUtil.readFile(banner.getJumpUrl());
			if (html != null && html.indexOf("</body>") != -1) {
				String content = html.split("<body>")[1].split("</body>")[0];
				banner.setContent(content);
			}
		}
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param banners
	 * @return
	 */
	@AutoLog(value = "Banners-添加")
	@ApiOperation(value="Banners-添加", notes="Banners-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Banners banners) {

		//生成bannerHtml
		String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String disrPath = webappPath+ File.separator+"BannersHTML"+ File.separator+ nowday;
		String fileName = "index_" + System.currentTimeMillis();
		String  editorContent=banners.getContent();
		MakeHtmlUtil.MakeHtml(editorContent,disrPath,fileName,mbPath);
		String jumpUrl = disrPath+File.separator+fileName+".html";

		banners.setJumpUrl(jumpUrl);
		bannersService.save(banners);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param banners
	 * @return
	 */
	@AutoLog(value = "Banners-编辑")
	@ApiOperation(value="Banners-编辑", notes="Banners-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Banners banners) {
		//生成bannerHtml
		String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String disrPath = webappPath+ File.separator+"BannersHTML"+ File.separator+ nowday;
		String fileName = "index_" + System.currentTimeMillis();
		String  editorContent=banners.getContent();
		MakeHtmlUtil.MakeHtml(editorContent,disrPath,fileName,mbPath);
		String jumpUrl = disrPath+File.separator+fileName+".html";
		banners.setJumpUrl(jumpUrl);
		bannersService.updateById(banners);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "Banners-通过id删除")
	@ApiOperation(value="Banners-通过id删除", notes="Banners-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		bannersService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "Banners-批量删除")
	@ApiOperation(value="Banners-批量删除", notes="Banners-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bannersService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "Banners-通过id查询")
	@ApiOperation(value="Banners-通过id查询", notes="Banners-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Banners banners = bannersService.getById(id);
		return Result.ok(banners);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param banners
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, Banners banners) {
      return super.exportXls(request, banners, Banners.class, "Banners列表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, Banners.class);
  }

}
